
/**
 * Content: Volatile用作标志位
 * --------------------------------------------------
 * Java语言中，关键字Volatile的使用你清楚吗？: https://juejin.cn/post/7169239464944861221
 */

public class java_1116_volatile {

    private static volatile int isDoorOpen = 0;  // 属于具体实例

    public static void main(String[] args) {

        // Volatile用作标志位
        System.out.println("--------------------Volatile用作标志位--------------------");

        Thread thread1 = new Thread(new OpenThread());
        Thread thread2 = new Thread(new CloseThread());

        thread1.start();
        thread2.start();

    }

    static class OpenThread implements Runnable {
        public void openDoor() {
            isDoorOpen = 1;
        }

        @Override
        public void run() {
            openDoor();
        }
    }
    static class CloseThread implements Runnable {
        public void closeDoor() {
            isDoorOpen = 0;
        }

        @Override
        public void run() {
            closeDoor();
        }
    }
}
